﻿
namespace JLL.China.Infrastructure.Repositories.ProjectNames
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Linq;
    using JLL.China.Model;
    public  class ProjectNamesRepository : SQLRepositoryBase<Model.ProjectName, int>, IProjectNamesRepository
    {
        public override Model.ProjectName FindBy(int key)
        {
            var item = DataContext.ProjectNames.SingleOrDefault(x => key.Equals(x.ProjectName_ID));
            if (item != null)
            {
                return ProjectNameFactory.FromLINQObject(item);
            }
            else
            {
                return null;
            }
        }

        public override IList<Model.ProjectName> FindAll()
        {
            throw new NotImplementedException();
        }

        protected override void PersistNewItem(Model.ProjectName item)
        {
            DataContext.ProjectNames.InsertOnSubmit(ProjectNameFactory.ToLINQObject(item));
        }

        protected override void PersistUpdatedItem(Model.ProjectName item)
        {
            var existNewsItem = DataContext.ProjectNames.SingleOrDefault(x => item.ProjectNameID.Equals(x.ProjectName_ID));
            if (existNewsItem != null)
            {
                existNewsItem.ProjectName_ID = item.ProjectNameID;
                
                existNewsItem.ProjectName_ID = item.ProjectNameID;
                existNewsItem.LanguageFID = item.LanguageFID;
                existNewsItem.Value = item.Value;
                
            }
            else
            {
                throw new Exception("ProjectName not found to update");
            }
        }

        protected override void PersistDeletedItem(Model.ProjectName item)
        {
            var existItem = DataContext.ProjectNames.SingleOrDefault(x=>item.ID.Equals(item.ID));

            if(existItem != null)
            {
                DataContext.ProjectNames.DeleteOnSubmit(existItem);
            }
        }

        

    }
}
